package com.jhgsys.internal.incentive.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.incentive.entity.IncentiveQuotaGoods;
import com.jhgsys.internal.incentive.service.IIncentiveQuotaGoodsService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 *  Controller
 *
 * @author zhangwei
 * @date 2024-09-02 10:39:13
 */
@Slf4j
@Validated
@Controller
public class IncentiveQuotaGoodsController extends BaseController {

    @Autowired
    private IIncentiveQuotaGoodsService incentiveQuotaGoodsService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveQuotaGoods")
    @RequiresPermissions("incentiveQuotaGoods:view")
    public String incentiveQuotaGoodsIndex(){
        return FebsUtil.view("incentive/incentiveQuotaGoods/incentiveQuotaGoodsList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveQuotaGoods/add")
    @RequiresPermissions("incentiveQuotaGoods:add")
    public String incentiveQuotaGoodsAdd(){
        return FebsUtil.view("incentive/incentiveQuotaGoods/incentiveQuotaGoodsEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveQuotaGoods/update/{id}")
    @RequiresPermissions("incentiveQuotaGoods:edit")
    public String incentiveQuotaGoodsUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("incentiveQuotaGoods", incentiveQuotaGoodsService.findIncentiveQuotaGoodsById(id));
        return FebsUtil.view("incentive/incentiveQuotaGoods/incentiveQuotaGoodsEdit");
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-02 10:39:13
     */
    @GetMapping("incentiveQuotaGoods")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaGoods:view")
    public FebsResponse getAllIncentiveQuotaGoodss(IncentiveQuotaGoods incentiveQuotaGoods) {
        return new FebsResponse().success().data(incentiveQuotaGoodsService.findIncentiveQuotaGoodsList(incentiveQuotaGoods));
    }

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-02 10:39:13
     */
    @GetMapping("incentiveQuotaGoods/list")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaGoods:view")
    public FebsResponse incentiveQuotaGoodsList(QueryRequest request, IncentiveQuotaGoods incentiveQuotaGoods) {
        Map<String, Object> dataTable = getDataTable(this.incentiveQuotaGoodsService.findIncentiveQuotaGoodsPage(request, incentiveQuotaGoods));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 IncentiveQuotaGoods
     * @author zhangwei
     * @date 2024-09-02 10:39:13
     */
    @Log("新增IncentiveQuotaGoods")
    @PostMapping("incentiveQuotaGoods")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaGoods:add")
    public FebsResponse addIncentiveQuotaGoods(@Valid IncentiveQuotaGoods incentiveQuotaGoods) throws FebsException {
        try {
            incentiveQuotaGoods.setCreatedBy(getCurrentUser().getUserName());
            this.incentiveQuotaGoodsService.createIncentiveQuotaGoods(incentiveQuotaGoods);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增IncentiveQuotaGoods失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 IncentiveQuotaGoods
     * @author zhangwei
     * @date 2024-09-02 10:39:13
     */
    @Log("逻辑删除IncentiveQuotaGoods")
    @GetMapping("incentiveQuotaGoods/delete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaGoods:delete")
    public FebsResponse deleteIncentiveQuotaGoods(@PathVariable("id") String incentiveQuotaGoodsIds) throws FebsException {
        try {
            IncentiveQuotaGoods incentiveQuotaGoods = new IncentiveQuotaGoods();
            incentiveQuotaGoods.setDeletedBy(getCurrentUser().getUserName());
            incentiveQuotaGoods.setDeletedTime(new Date());
            incentiveQuotaGoods.setDeletedFlg(FebsConstant.INVALID);
            this.incentiveQuotaGoodsService.deleteIncentiveQuotaGoods(incentiveQuotaGoods,incentiveQuotaGoodsIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveQuotaGoods失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 IncentiveQuotaGoods
    * @author zhangwei
    * @date 2024-09-02 10:39:13
    */
    @Log("物理删除IncentiveQuotaGoods")
    @GetMapping("incentiveQuotaGoods/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaGoods:delete")
    public FebsResponse physicallyDeleteIncentiveQuotaGoods(@PathVariable("id") String incentiveQuotaGoodsIds) throws FebsException {
        try {
            this.incentiveQuotaGoodsService.physicallyDeleteIncentiveQuotaGoods(incentiveQuotaGoodsIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveQuotaGoods失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 IncentiveQuotaGoods
     * @author zhangwei
     * @date 2024-09-02 10:39:13
     */
    @Log("修改IncentiveQuotaGoods")
    @PostMapping("incentiveQuotaGoods/update")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaGoods:edit")
    public FebsResponse updateIncentiveQuotaGoods(IncentiveQuotaGoods incentiveQuotaGoods) throws FebsException {
        try {
            incentiveQuotaGoods.setUpdatedBy(getCurrentUser().getUserName());
            this.incentiveQuotaGoodsService.updateIncentiveQuotaGoods(incentiveQuotaGoods);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改IncentiveQuotaGoods失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

}
